import {ElMessage} from "element-plus";
import fa from "element-plus/packages/locale/lang/fa";
export default {
	getQueryVariable(variable)
	{
		var query = window.location.search.substring(1)
		var vars = query.split("&")
		for (var i=0;i<vars.length;i++) {
			var pair = vars[i].split("=")
			if(pair[0] === variable){return pair[1]}
		}
		return false
	},
	isMobile(str) {
		let rule = /^1\d{10}$/
		return rule.test(str)
	},
	padLeftZero(str) {
		return ('00' + str).substr(str.length)
	},
	formatDate(unix_timestamp, fmt = 'yyyy-MM-dd hh:mm:ss') {
		let date = new Date(unix_timestamp*1000)
		if (/(y+)/.test(fmt)) {
			fmt = fmt.replace(RegExp.$1, (date.getFullYear() + '').substr(4 - RegExp.$1.length))
		}
		let o = {
			'M+': date.getMonth() + 1,
			'd+': date.getDate(),
			'h+': date.getHours(),
			'm+': date.getMinutes(),
			's+': date.getSeconds()
		}
		for (let k in o) {
			if (new RegExp(`(${k})`).test(fmt)) {
				let str = o[k] + ''
				fmt = fmt.replace(RegExp.$1, (RegExp.$1.length === 1) ? str : this.padLeftZero(str))
			}
		}
		return fmt
	},

	checkImage(file,maxsize = 512) {
		if(file.size > maxsize*1024) {
			ElMessage.error('图片大小不能超过' + maxsize + 'Kb')
			return false
		}
		let filename = file.name
		let arr = filename.split('.')
		let ext = arr.length > 1 ?  arr.pop() : ''
		let allowType = [ 'image/jpeg', 'image/bmp' ,'image/gif' ,'image/png','image/jpg','image/pjpeg','image/x-png','image/webp']
		if(allowType.indexOf(file.type) === -1) {
			ElMessage.error('不支持的文件类型:' + file.type)
			return false
		}
		return ext
	}


}